/**
 *   Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.

 *   \author   Haichao.chc
 *   \date     Oct 2020
 *   \brief    Simple condition wait and notify class
 */

#pragma once

#include <chrono>
#include <condition_variable>
#include <mutex>

namespace proxima {
namespace be {

/*
 * Simple wait notifier for multi-threads message sync
 */
class WaitNotifier {
 public:
  //! Constructor
  WaitNotifier() = default;

  //! Destructor
  ~WaitNotifier() = default;

 public:
  // Wait for condition var notify
  void wait();

  // Wait for deserted time
  void wait_until(const std::chrono::system_clock::time_point &tm_point);

  // Wait for some duration time
  void wait_for(const std::chrono::system_clock::duration &tm_duration);

  // Notify once
  void notify();

 private:
  bool notified_{false};
  std::mutex mutex_;
  std::condition_variable cv_;
};

}  // namespace be
}  // end namespace proxima
